<?php
/**
 * Sharing World default controller
 * 
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/
 * @license		GNU/GPL
 */

jimport('joomla.application.component.controller');

/**
 * Sharing World Component Controller
 *
 * @package		SharingWorld
 */
class SharingController extends JController
{
	/**
	 * Method to display the view
	 *
	 * @access	public
	 */
	function display()
	{
		parent::display();
	}
	
	/**
	* Edit a weblink and show the edit form
	*
	* @acces public
	* @since 1.5
	*/
	function add()
	{
		$user = & JFactory::getUser();

		// Make sure you are logged in
		/*
		if ($user->get('aid', 0) < 1) {
			JError::raiseError( 403, JText::_('ALERTNOTAUTH') );
			return;
		}
		*/

		JRequest::setVar('view', 'sharing');
		JRequest::setVar('layout', 'form');

		parent::display();
	}

	function save()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		// Get some objects from the JApplication
		$db		=& JFactory::getDBO();
		$user	=& JFactory::getUser();

		/*
		// Must be logged in
		if ($user->get('id') < 1) {
			JError::raiseError( 403, JText::_('ALERTNOTAUTH') );
			return;
		}
		*/

		//get data from the request
		$post = JRequest::getVar('jform', array(), 'post', 'array');
		
		
		$model = $this->getModel('sharing');

		if ($model->store($post)) {
			$msg = JText::_( 'Booking Saved' );
		} else {
			$msg = JText::_( 'Error Saving Booking' );
		}

		// Check the table in so it can be edited.... we are done with it anyway
		//$model->checkin();

		// admin users gid
		$gid = 25;

		// list of admins
		$query = 'SELECT email, name' .
				' FROM #__users' .
				' WHERE gid = ' . $gid .
				' AND sendEmail = 1';
		$db->setQuery($query);
		if (!$db->query()) {
			JError::raiseError( 500, $db->stderr(true));
			return;
		}
		$adminRows = $db->loadObjectList();

		// send email notification to admins
		foreach ($adminRows as $adminRow) {
			JUtility::sendAdminMail($adminRow->name, $adminRow->email, '',  JText::_('Prayer time'), $post['group'] . "\n" . $post['date'] . $post['hour'], $user->get('username'), JURI::base());
		}
		$arraydate = explode("/", $post['date']);
		$realdate = mktime(0,0,0, $arraydate['1'],$arraydate['0'],$arraydate['2']);
		$this->setRedirect(JRoute::_('index.php?option=com_sharing&date='.$realdate, false), $msg);	
	}
	
}
?>
